package database;

import java.sql.ResultSet;
import java.sql.SQLException;

import capsules.CapsuleSpecification;

import com.Ostermiller.util.Base64;

public class DatabaseOperationsCapsules {
	
	
	private ConnectionDB conn;
			

	public DatabaseOperationsCapsules(ConnectionDB conn){
		this.conn = conn;
	}
	
	
	public CapsuleSpecification getCapsuleForReplication()
            throws SQLException{

		ResultSet result = conn.executeSelect("call capsuleSpecificationReplication()");

		if(!result.next()){
            
            setAllCapsuleUnReplicated();

             result = conn.executeSelect("call capsuleSpecificationReplication()");
             result.next();
        }

		CapsuleSpecification capsuleSpecification = new CapsuleSpecification(
                result.getString("name"), result.getLong("length"));


		result.close();
        conn.closeStatement();

		return capsuleSpecification;
	}

	
	public void setCapsuleReplicated(String nameCapsule)
            throws SQLException{
		conn.executeUpdate("call setReplicated('"+nameCapsule+"')");
	}

   private void  setAllCapsuleUnReplicated()
            throws SQLException{
		conn.executeUpdate("call setAllUnreplicated()");
	}
	
	
	public CapsuleSpecification getCapsuleSpecification(String nameCapsule)
            throws SQLException{

		CapsuleSpecification capsuleSpecification = null;

		ResultSet resultQuery = conn.executeSelect("call capsuleSpecification('"+nameCapsule+"')");

		if(resultQuery.next()){

			byte [] hashCap = Base64.decodeToBytes(resultQuery.getString("hash"));

			capsuleSpecification = new CapsuleSpecification(nameCapsule,
                    resultQuery.getLong("length"), hashCap);
		}

		resultQuery.close();		
		conn.closeStatement();

		return capsuleSpecification;
	}
	
	
	public void insertCapsule(String nameCapsule, long lengthCapsule, String hashCapsule)
            throws SQLException{
		conn.executeUpdate("call insertCapsule('"+nameCapsule+"',"+lengthCapsule+",'"+hashCapsule+"')");
	}
	
	
	public boolean existsCapsule(String nameCapsule)
            throws SQLException{

		ResultSet resultQuery = conn.executeSelect("call existsCapsule('"+nameCapsule+"')");

		boolean exists = false;

		if(resultQuery.next())
			exists = true;

		resultQuery.close();		
		conn.closeStatement();

		return exists;		 
	}

	
	public boolean isCapsuleRestore(String nameCapsule)
            throws SQLException{

		boolean isRestore = false;

		ResultSet result = conn.executeSelect("call isCapsuleRestored('"+nameCapsule+"')");

		if(result.next())			 
			isRestore = result.getBoolean("isRestored");

		result.close();
		conn.closeStatement();

		return isRestore;
	}


	public  long getLengthCapsule(String nameCapsule)
            throws SQLException{
		
		ResultSet result = conn.executeSelect("call lengthCapsule('"+nameCapsule+"')");

		result.next();

		long length = result.getLong("length");

		result.close();
		conn.closeStatement();

		return length;
	}
	
	
	public void setCapsuleRestore(String nameCapsule)
            throws SQLException{
		conn.executeUpdate("call setCapsuleRestored('"+nameCapsule+"') ");
	}


	public void close()
            throws SQLException{
		if(conn!=null){
			conn.close();
		}
	}
	
	
}
